package com.csmht.tododemo.controller.user;

import com.alibaba.excel.EasyExcel;
import com.csmht.tododemo.annotation.RoleIntercept;
import com.csmht.tododemo.constant.MessageConstant;
import com.csmht.tododemo.exiception.ServiceException;
import com.csmht.tododemo.pojo.dto.PageDTO;
import com.csmht.tododemo.pojo.dto.ToDoDTO;
import com.csmht.tododemo.pojo.entity.TodoExcel;
import com.csmht.tododemo.pojo.enumeration.RoleEnum;
import com.csmht.tododemo.pojo.vo.ToDoVO;
import com.csmht.tododemo.result.Result;
import com.csmht.tododemo.service.TodoService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RestController("userTodoController")
@RequestMapping("/user/todo")
@Slf4j
@CrossOrigin
@RoleIntercept(roles = {RoleEnum.ADMIN, RoleEnum.USER,RoleEnum.BAN_USER})
public class TodoController {

    @Resource
    private TodoService todoService;

    @PutMapping
    public Result<Object> addTodo(@RequestBody ToDoDTO toDoDTO){

        todoService.add(toDoDTO);

        return Result.success();
    }

    @DeleteMapping
    public Result<Object> delTodo(@RequestBody ToDoDTO toDoDTO){
        todoService.del(toDoDTO);
        return Result.success();
    }

    @PostMapping
    public Result<Object> update(@RequestBody ToDoDTO dto){
        todoService.update(dto);
        return Result.success();
    }

    @PostMapping("/list")
    @RoleIntercept(roles = {RoleEnum.USER})
    public Result<List<ToDoVO>> list(@RequestBody PageDTO pageDTO){

        List<ToDoVO> list = todoService.list(pageDTO);

        return Result.success(list);
    }

    @GetMapping("/{id}")
    @CrossOrigin
    public Result<ToDoVO> getById(@PathVariable Long id){
        ToDoVO ans =  todoService.getById(id);
        return Result.success(ans);
    }

    @PostMapping("/status")
    public  Result<Object> status(@RequestBody ToDoDTO toDoDTO){
        todoService.status(toDoDTO);

        return Result.success();
    }

    @PutMapping("/data")
    private Result<Object> data(@RequestParam MultipartFile file){
        todoService.data(file);
        return Result.success();
    }

    @GetMapping("/data")
    private Result<byte[]> data(){
        byte[] export = todoService.export();
        return Result.success(export);
    }

    @RoleIntercept(roles = {RoleEnum.USER,RoleEnum.ADMIN})
    @PutMapping("/put/{todoId}")
    public Result<String> put(@PathVariable Long todoId){
        String url = todoService.put(todoId);
        return Result.success(url);
    }

    @DeleteMapping("/put/{todoId}")
    public Result<String> delete(@PathVariable Long todoId){
        todoService.delVisitor(todoId);
        return Result.success();
    }

}
